﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RecentFile.cs" >
//   J. Prinz
// </copyright>
// <summary>
//   Represents a recent file.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace AutomationMLMapper.Utilities
{
    using AutomationMLMapper.MVVM_Framework;
    using System;
    using System.ComponentModel;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    /// <summary>
    ///   Represents a recent file.
    /// </summary>
    public sealed class RecentFile : PropertyChangedBase, IXmlSerializable
    {
        #region Fields

        /// <summary>
        ///  <see cref="IsPinned"/>
        /// </summary>
        private bool isPinned;

        /// <summary>
        ///  <see cref="Path"/>
        /// </summary>
        private string path;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="RecentFile"/> class. 
        /// This constructor overload is reserved and should not be used. It is used internally by the XmlSerializer.
        /// </summary>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public RecentFile()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RecentFile"/> class. 
        /// </summary>
        /// <param name="path">
        /// The file path. 
        /// </param>
        public RecentFile(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentException("The argument path must not be null or empty.");
            }

            this.IsPinned = false;
            this.path = path;
        }

        #endregion

        #region Public Properties

        /// <summary>
        ///   Gets or sets a value indicating whether this recent file is pinned.
        /// </summary>
        public bool IsPinned
        {
            get
            {
                return this.isPinned;
            }

            set
            {
                if (this.isPinned != value)
                {
                    this.isPinned = value;
                    this.NotifyOfPropertyChange();
                }
            }
        }

        /// <summary>
        ///   Gets the file path.
        /// </summary>
        public string Path
        {
            get
            {
                return this.path;
            }
        }

        #endregion

        #region Explicit Interface Methods

        /// <summary>
        ///   Explicit Interface Method
        /// </summary>
        /// <returns> The System.Xml.Schema.XmlSchema. </returns>
        XmlSchema IXmlSerializable.GetSchema()
        {
            return null;
        }

        /// <summary>
        /// Explicit Interface Method to read xml.
        /// </summary>
        /// <param name="reader">
        /// The reader. 
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Argument is null.
        /// </exception>
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            this.IsPinned = false; // bool.Parse(reader.GetAttribute("IsPinned"));
            this.path = reader.ReadElementContentAsString();
        }

        /// <summary>
        /// Explicit Interface Method to write xml.
        /// </summary>
        /// <param name="writer">
        /// The writer. 
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// Argument is null.
        /// </exception>
        void IXmlSerializable.WriteXml(XmlWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            writer.WriteAttributeString("IsPinned", this.IsPinned.ToString());
            writer.WriteString(this.Path);
        }

        #endregion
    }
}